package com.cndw.kungfu.controller;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;

import com.cndw.commons.util.BlankUtils;
import com.cndw.commons.util.DateFormatUtils;
import com.cndw.kungfu.comm.AccessFlag;
import com.cndw.kungfu.comm.PositionInspection;
import com.cndw.kungfu.comm.tencent.LogFieldType;
import com.cndw.kungfu.comm.tencent.QQDC;
import com.cndw.kungfu.config.Constants;
import com.cndw.kungfu.config.ConstantsChat;
import com.cndw.kungfu.config.ConstantsConfig;
import com.cndw.kungfu.config.ConstantsCost;
import com.cndw.kungfu.config.ConstantsDayIntKey;
import com.cndw.kungfu.config.ConstantsFlag;
import com.cndw.kungfu.config.ConstantsIntKey;
import com.cndw.kungfu.config.ConstantsLevel;
import com.cndw.kungfu.config.ConstantsLog;
import com.cndw.kungfu.config.ConstantsModel;
import com.cndw.kungfu.config.ConstantsRadio;
import com.cndw.kungfu.domain.GoodsMode;
import com.cndw.kungfu.domain.MonsterDeploy;
import com.cndw.kungfu.domain.Player;
import com.cndw.kungfu.domain.Scene;
import com.cndw.kungfu.domain.Union;
import com.cndw.kungfu.domain.UnionAsset;
import com.cndw.kungfu.domain.UnionCashCow;
import com.cndw.kungfu.domain.UnionConfig;
import com.cndw.kungfu.domain.UnionMember;
import com.cndw.kungfu.domain.UnionSkillConfig;
import com.cndw.kungfu.event.once.PlayerStateTask;
import com.cndw.kungfu.ext.LangConfigLib;
import com.cndw.kungfu.ext.LangUtil;
import com.cndw.kungfu.ext.LanguageLib;
import com.cndw.kungfu.ext.LogUtil;
import com.cndw.kungfu.ext.NumberProcesser;
import com.cndw.kungfu.ext.TimeProcesser;
import com.cndw.kungfu.ext.WordProcesser;
import com.cndw.kungfu.model.Context;
import com.cndw.kungfu.model.DataSerialize;
import com.cndw.kungfu.model.battle.Battle;
import com.cndw.kungfu.model.battle.FighterMonster;
import com.cndw.kungfu.model.pve.UnionPet;
import com.cndw.kungfu.model.pve.UnionPetBattle;
import com.cndw.kungfu.model.pve.domain.UnionPetConfig;
import com.cndw.kungfu.model.siege.domain.SiegeWar;
import com.cndw.kungfu.model.sys.ChatMain;
import com.cndw.kungfu.model.sys.LogServer;
import com.cndw.kungfu.model.union.UnionManager;
import com.cndw.kungfu.model.union.UnionShopConfig;
import com.cndw.kungfu.vo.EventNotice;
import com.cndw.kungfu.vo.UnionConfigVO;
import com.cndw.kungfu.vo.UnionMemberVO;
import com.cndw.kungfu.vo.UnionVO;
import com.cndw.rpg.annotation.Controller;
import com.cndw.rpg.framework.ResponseMessage;
import com.cndw.rpg.framework.Session;

/**
 * 帮派入口
 */
@Controller
public class UnionController {

	@Resource
	private UnionManager unionManager;

	/**
	 * 获取帮派列表
	 * 
	 * @param session
	 * @return
	 */
	public ResponseMessage getUnionList(Session session, Map<String, Object> paramMap) {

		String search = paramMap.containsKey("search") && !StringUtils.isBlank(paramMap.get("search").toString()) ? "%" + paramMap.get("search").toString()
				+ "%" : null;
		int page = paramMap.containsKey("page") ? Integer.parseInt(paramMap.get("page").toString()) : 1;
		int pageSize = paramMap.containsKey("pageSize") ? Integer.parseInt(paramMap.get("pageSize").toString()) : Constants.PAGE_SIZE;

		List<UnionVO> unionList = unionManager.getUnionList(session.getUid(), (page - 1) * pageSize, pageSize, search);
		int unionCounts = unionManager.getUnionListCount(search);

		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("unionList", unionList);
		resultMap.put("unionCounts", unionCounts);
		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
		QQDC.getInstance().logClick(player, LogFieldType.unionList);

		return ResponseMessage.getSuccMessage(resultMap);
	}

	/**
	 * 获取创建帮派需要的银两
	 * 
	 * @param session
	 * @return
	 */
	public ResponseMessage getCreateUnionAsset(Session session) {
		return ResponseMessage.getSuccMessage(ConstantsConfig.getUnionCreateAsset());
	}

	/**
	 * 建立帮派
	 * 
	 * @param session
	 *            当前会话
	 * @param param
	 *            帮派参数
	 * @return 返回执行结果
	 */
	public ResponseMessage createUnion(Session session, Map<String, Object> paramMap) {

		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());

		if (player.getLevel() < ConstantsLevel.lv23UnionCreate) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("union.create.level", ConstantsLevel.lv23UnionCreate));
		}

		if (player.getUnionId() > 0) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("union.create.exist"));
		}

		if (unionManager.checkQuitCountsLimit(player)) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("union.create.quit"));
		}

		if (player.getAsset() < ConstantsConfig.getUnionCreateAsset()) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("union.create.asset", ConstantsConfig.getUnionCreateAsset()));
		}

		if (!paramMap.containsKey("unionName")) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("union.create.nameEmtpy"));
		}

		String unionName = paramMap.get("unionName").toString();
		if (unionName.getBytes(Constants.charset).length < ConstantsConfig.getUnionNameLength()
				|| unionName.getBytes(Constants.charset).length > ConstantsConfig.getUnionNameLengthMax()) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("union.create.nameLength"));
		}

		if (!WordProcesser.chkPerg(unionName, ConstantsConfig.getCommCreateNameExp())) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("union.create.nameReg"));
		}

		String badWord = WordProcesser.chkBadWorlds(unionName);
		if (badWord != null) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("union.create.badname", badWord));
		}

		if (unionManager.checkUnionExist(unionName)) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("union.create.nameExist"));
		}
		Union union;
		UnionMember unionMemeber;
		synchronized (player) {
			if (player.getUnionId() > 0) {
				return null;
			}
			
			union = unionManager.insertUnion(unionName, player.getPlayerId(), player.getPlayerName());
			if (union == null) {
				return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.sys"));
			}
			player.alterAsset(-ConstantsConfig.getUnionCreateAsset());
			unionMemeber = unionManager.setUnionIdAdd(player, union.getId(), 6);
		}


		Map<String, Object> map = new HashMap<String, Object>();
		map.put("unionId", union.getId());
		map.put("unionName", union.getName());
		map.put("asset", player.getAsset());
		map.put("flag", unionMemeber.getFlag());
		map.put("permission", ConstantsConfig.getUnionJobsFlag()[unionMemeber.getPosition()]);
		player.commitSync();// 提交同步

		// 添加“帮派-创建帮派”日志
		LogServer.recordLog(player, ConstantsLog.union, 60001, union.getName(), ConstantsConfig.getUnionCreateAsset());

		// 添加“帮派-加入帮派”日志
		LogServer.recordLog(player, ConstantsLog.union, 60016, player.getPlayerName(), union.getName());

		// ChatMain.systemMessage(LanguageLib.get("chat.board.union",
		// player.getPlayerName(), union.getName()));
		ChatMain.systemMessage(LanguageLib.get("chat.board.union", LangUtil.makeNameLink(player), union.getName()));

		Context.getTaskServer().autoRaidioTask(player.getPlayerId(), 1);

		return ResponseMessage.getSuccMessage(map);
	}

	/**
	 * 查看帮派
	 * 
	 * @param session
	 *            当前会话
	 * @return 返回帮派信息
	 */
	public ResponseMessage showUnion(Session session) {

		Union union = Context.getUnionServer().getUnionByPlayerId(session.getUid());

		if (union == null) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.args"));
		}

		Map<String, Object> resultMap = new HashMap<String, Object>();
		UnionVO unionVO = union.toUnionVO();
		UnionConfig config = Context.getUnionServer().getUnionConfigByLevel(unionVO.getLevel());
		resultMap.put("maxCapacity", config.getCapacity());
		resultMap.put("maxAsset", config.getAssetLimit());

		List<UnionMember> members = union.getMembers();
		List<UnionMemberVO> memberVos = new ArrayList<UnionMemberVO>();
		for (UnionMember member : members) {
			memberVos.add(member.toUnionMemberVO());
		}

		// 帮主在指定时间未上线，自动换帮主
		Player masterPlayer = Context.getPlayerServer().getByPlayerId(union.getPlayerId());
		if (members.size() >= 2 && masterPlayer.getOffLineTime() > 0
				&& (TimeProcesser.getUnixTime() - masterPlayer.getOffLineTime()) > ConstantsModel.unionAbdicateTime
				&& (TimeProcesser.getUnixTime() - masterPlayer.getLastLogin()) > ConstantsModel.unionAbdicateTime) {

			List<UnionMember> membersSecond = new ArrayList<UnionMember>(members);
			Iterator<UnionMember> it = membersSecond.iterator();
			int currentTime = TimeProcesser.getUnixTime();
			while (it.hasNext()) {
				UnionMember unionMember = (UnionMember) it.next();
				Player aPlayer = Context.getPlayerServer().getByPlayerId(unionMember.getPlayerId());
				if (aPlayer == null) {
					it.remove();
				} else if (aPlayer.getOffLineTime() > 0 && aPlayer.getOffLineTime() + ConstantsModel.unionAbdicateTime < currentTime) {
					it.remove();
				}
			}
			if (membersSecond.size() >= 1) {
				// 历史贡献度排序
				Collections.sort(membersSecond, new Comparator<UnionMember>() {
					@Override
					public int compare(UnionMember o1, UnionMember o2) {
						if (o1.getHissource() == o2.getHissource()) {
							return 0;
						} else if (o1.getHissource() > o2.getHissource()) {
							return -1;
						} else {
							return 1;
						}
					}
				});

				UnionMember accedeMember = membersSecond.get(0);
				if (accedeMember.getPlayerId() == masterPlayer.getPlayerId() && membersSecond.size() > 1) {
					accedeMember = membersSecond.get(1);
				}
				LogUtil.getMain().debug(union.getId() + ":" + masterPlayer.getPlayerId() + "->" + accedeMember.getPlayerId());
				if (accedeMember.getPlayerId() != masterPlayer.getPlayerId() && accedeMember.getUnionId() == masterPlayer.getUnionId()) {
					Player accedePlayer = Context.getPlayerServer().getByPlayerId(accedeMember.getPlayerId());
					union.setPlayerId(accedePlayer.getPlayerId());
					union.setPlayerName(accedePlayer.getPlayerName());
					accedeMember.setPosition(6);

					UnionMember abdicateMember = union.getMember(masterPlayer.getPlayerId());
					abdicateMember.setPosition(0);
					unionManager.abdicate(union, accedeMember, abdicateMember);

					// 添加”帮派-禅让“日志
					LogServer.recordLog(masterPlayer, ConstantsLog.union, 60009, masterPlayer.getPlayerName(), accedePlayer.getPlayerName());
				}
			}

		}

		// 在线离线排序
		Collections.sort(memberVos, new Comparator<UnionMemberVO>() {
			@Override
			public int compare(UnionMemberVO o1, UnionMemberVO o2) {
				if (o1.getPlayerStatus() == o2.getPlayerStatus()) {
					return 0;
				} else if (o1.getPlayerStatus()) {
					return 1;
				} else {
					return -1;
				}
			}
		});

		// 职位，声望，历史声望排序
		Collections.sort(memberVos);

		resultMap.put("union", unionVO);
		resultMap.put("members", memberVos);
		resultMap.put("membersRes", union.getMember(session.getUid()).getResource());
		resultMap.put("current", union.getMember(session.getUid()).toUnionMemberVO());

		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
		QQDC.getInstance().logClick(player, LogFieldType.unionShow);
		return ResponseMessage.getSuccMessage(resultMap);

	}

	/**
	 * 解散帮派
	 * 
	 * @param session
	 *            当前会话
	 * @return 返回执行结果
	 */
	public ResponseMessage dismissUnion(Session session) {

		Union union = Context.getUnionServer().getUnionByPlayerId(session.getUid());

		if (union == null) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.args"));
		}

		if (unionManager.checkPermission(session.getUid(), Union.DISMISS)) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("union.dismiss.perm"));
		}

		String unionName = union.getName();
		unionManager.dismissUnion(union, LanguageLib.get("union.dismiss.title"), LanguageLib.get("union.dismiss.content"));
		// 添加”帮派-解散帮派“日志
		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
		LogServer.recordLog(player, ConstantsLog.union, 60008, player.getPlayerName(), unionName);

		return ResponseMessage.getSuccMessage(true);
	}

	/**
	 * 禅让帮主之位
	 * 
	 * @param session
	 *            当前会话
	 * @param accedeId
	 *            继位者id
	 * @return 返回执行结果
	 */
	public ResponseMessage abdicate(Session session, long accedeId) {

		Union union = Context.getUnionServer().getUnionByPlayerId(session.getUid());

		if (union == null) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.args"));
		}
		if (session.getUid() == accedeId) {
			return null;
		}

		if (unionManager.checkPermission(session.getUid(), Union.ABDICATE)) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("union.abdicate.perm"));
		}

		UnionMember accedeMember = union.getMember(accedeId);
		if (accedeMember == null) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("union.abdicate.accede"));
		}

		Player accedePlayer = Context.getPlayerServer().getByPlayerId(accedeId);
		union.setPlayerId(accedePlayer.getPlayerId());
		union.setPlayerName(accedePlayer.getPlayerName());
		accedeMember.setPosition(6);

		UnionMember abdicateMember = union.getMember(session.getUid());
		abdicateMember.setPosition(0);

		unionManager.abdicate(union, accedeMember, abdicateMember);

		// 添加”帮派-禅让“日志
		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
		LogServer.recordLog(player, ConstantsLog.union, 60009, player.getPlayerName(), accedePlayer.getPlayerName());

		return ResponseMessage.ticpMsg(LanguageLib.get("comm.suc.exec"));
	}

	/**
	 * 开除成员
	 * 
	 * @param session
	 *            当前会话
	 * @param expelId
	 *            开除者的Id
	 * @return 返回执行结果
	 */
	public ResponseMessage expelMember(Session session, long expelId) {

		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
		Union union = Context.getUnionServer().getUnionById(player.getUnionId());

		if (union == null) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.args"));
		}

		if (expelId == session.getUid()) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("union.expel.self"));
		}

		if (expelId == union.getPlayerId()) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("union.expel.main"));
		}

		if (unionManager.checkPermission(session.getUid(), Union.EXPEL)) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("union.expel.perm"));
		}

		UnionMember expelMember = union.removeMember(expelId);

		if (expelMember == null) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("union.expel.null"));
		}

		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("unionId", 0);
		resultMap.put("flag", 0);
		resultMap.put("permission", 0);
		resultMap.put("expel", true);
		Context.getTcpServer().broadcast(resultMap, ConstantsRadio.unionIdUpdateRadio, expelId);

		Player expelPlayer = Context.getPlayerServer().getByPlayerId(expelMember.getPlayerId());
		unionManager.setQuitCounts(expelPlayer);
		unionManager.deleteUnionMember(expelMember);
		SiegeWar.removePlayer(expelId); // 移除围城玩家
		String content = LanguageLib.get("union.expel.content", union.getName());
		Context.getMessageServer().sendMail(expelPlayer.getPlayerId(), expelPlayer.getPlayerName(), session.getUid(), player.getPlayerName(),
				LanguageLib.get("union.expel.title", union.getName()), content);

		// 添加”帮派-开除成员“日志
		LogServer.recordLog(player, ConstantsLog.union, 60010, player.getPlayerName(), expelPlayer.getPlayerName());

		// 添加”帮派-被踢出帮派“日志
		LogServer.recordLog(expelPlayer, ConstantsLog.union, 60017, player.getPlayerName(), union.getName());

		return ResponseMessage.getSuccMessage(true);
	}

	/**
	 * 邀请成员
	 * 
	 * @param session
	 *            当前会话
	 * @param inviteId
	 *            邀请的玩家Id
	 * @return 返回执行结果
	 */
	public ResponseMessage inviteMember(Session session, String playerName) {

		if (unionManager.checkPermission(session.getUid(), Union.INVITE)) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("union.invite.perm"));
		}

		Union union = Context.getUnionServer().getUnionByPlayerId(session.getUid());

		if (union == null) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.args"));
		}

		Player invitePlayer = Context.getPlayerServer().getByPlayerName(playerName);

		if (BlankUtils.isBlank(invitePlayer)) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("union.invite.null"));
		}

		if (!Context.getPlayerServer().getByPlayerId(invitePlayer.getPlayerId()).isOnline()) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("union.invite.offline"));
		}

		if (unionManager.checkUnionMemberLimit(union)) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("union.invite.limit"));
		}

		if (invitePlayer.getUnionId() > 0) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("union.invite.exist"));
		}

		if (unionManager.checkQuitCountsLimit(invitePlayer)) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("union.invite.quit"));
		}

		if (invitePlayer.getLevel() < ConstantsLevel.lv23UnionCreate) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("union.invite.level", ConstantsLevel.lv23UnionCreate));
		}

		int timestamp = TimeProcesser.getUnixTime();
		union.putInviteMember(invitePlayer.getPlayerId(), timestamp);

		// 邮件邀请修改为即时邀请
		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
		String content = LanguageLib.get("union.invite.content", player.getPlayerName(), union.getName());
		Object[] paras = new Object[] { union.getId() };

		// 确认回调函数处理邀请函数processInvite(...)
		Context.getTcpServer().broadcastConfirm(content, "UnionController", "processInvite", 3, paras, invitePlayer.getPlayerId());

		return ResponseMessage.ticpMsg(LanguageLib.get("union.invite.suc"));
	}

	/**
	 * 处理邀请
	 * 
	 * @param session
	 *            当前会话
	 * @param unionId
	 *            帮会id
	 * @param confirm
	 *            确认邀请
	 * @return 返回执行结果
	 */
	public ResponseMessage processInvite(Session session, int unionId, boolean confirm) {

		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());

		Union union = Context.getUnionServer().getUnionById(unionId);
		if (union == null) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.args"));
		}

		if (!union.containsInviteMember(session.getUid())) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.args"));
		}

		union.removeInviteMember(session.getUid());

		if (player.getUnionId() > 0 || unionId <= 0) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("union.apply.exist"));
		}

		if (player.getLevel() < ConstantsLevel.lv23UnionCreate) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("union.apply.level", ConstantsLevel.lv23UnionCreate));
		}

		if (unionManager.checkQuitCountsLimit(player)) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("union.apply.quit"));
		}

		if (!confirm) {
			return null;
		} else {
			if (unionManager.checkUnionMemberLimit(union)) {
				return ResponseMessage.ticpErrMsg(LanguageLib.get("union.apply.limit"));
			} else {
				UnionMember member;
				synchronized (player) {
					if (player.getUnionId() > 0) {
						return null;
					}
					member = unionManager.setUnionIdAdd(player, unionId, 0);
				}

				player.clacProperties();
				player.commitSync();
				Context.getTaskServer().autoRaidioTask(player.getPlayerId(), 1);

				// 添加“帮派-加入帮派”日志
				LogServer.recordLog(player, ConstantsLog.union, 60016, player.getPlayerName(), union.getName());

				Map<String, Object> resultMap = new HashMap<String, Object>();
				resultMap.put("unionId", unionId);
				resultMap.put("unionName", union.getName());
				resultMap.put("flag", member.getFlag());
				resultMap.put("permission", ConstantsConfig.getUnionJobsFlag()[member.getPosition()]);
				Context.getTcpServer().broadcast(resultMap, ConstantsRadio.unionIdUpdateRadio, player.getPlayerId());
				return ResponseMessage.ticpMsg(LanguageLib.get("union.invite.join", union.getName()));
			}
		}
	}

	/**
	 * 获取申请列表
	 * 
	 * @param session
	 *            当前会话
	 * @return 返回执行结果
	 */
	public ResponseMessage getApplyMembers(Session session) {

		Union union = Context.getUnionServer().getUnionByPlayerId(session.getUid());

		if (union == null) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.args"));
		}

		List<UnionMemberVO> members = new ArrayList<UnionMemberVO>();
		Map<Long, Integer> applyMap = DataSerialize.getInstance().getApplyMember().get(union.getId());
		if (!BlankUtils.isBlank(applyMap)) {
			Iterator<Long> iterator = applyMap.keySet().iterator();
			while (iterator.hasNext()) {
				Long playerId = iterator.next();
				int timestamp = applyMap.get(playerId);
				if ((TimeProcesser.getUnixTime() - timestamp) > ConstantsModel.unionApplyTime) {
					iterator.remove();
					DataSerialize.getInstance();
				} else {
					Player player = Context.getPlayerServer().getByPlayerId(playerId);
					members.add(UnionMemberVO.newUnionMemberVO(union, player, timestamp));
				}
			}
		}

		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("members", members);

		return ResponseMessage.getSuccMessage(resultMap);
	}

	/**
	 * 申请加入帮派
	 * 
	 * @param session
	 *            当前会话
	 * @param unionId
	 *            帮派Id
	 * @return 返回执行结果
	 */
	public ResponseMessage applyUnion(Session session, int unionId) {

		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());

		if (player.getUnionId() > 0 || unionId <= 0) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("union.apply.exist"));
		}

		if (unionManager.checkQuitCountsLimit(player)) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("union.apply.quit"));
		}

		if (player.getLevel() < ConstantsLevel.lv23UnionCreate) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("union.apply.level", ConstantsLevel.lv23UnionCreate));
		}

		if (player.getDataInfo().getApplyUnionSize() >= ConstantsModel.unionApplyCounts) {

			boolean canApply = false;
			Iterator<Integer> iterator = player.getDataInfo().getApplyUnions().iterator();
			while (iterator.hasNext()) {
				Integer timestamp = player.getDataInfo().getApplyUnion(iterator.next());
				if ((TimeProcesser.getUnixTime() - timestamp) > ConstantsModel.unionApplyTime) {
					canApply = true;
					iterator.remove();
					break;
				}
			}

			if (!canApply)
				return ResponseMessage.ticpErrMsg(LanguageLib.get("union.apply.max", ConstantsModel.unionApplyCounts));
		}

		Union union = Context.getUnionServer().getUnionById(unionId);

		if (union == null) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.args"));
		}

		if (unionManager.checkUnionMemberLimit(union)) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("union.apply.limit"));
		}

		switch (union.getValidMode()) {
		case 1:
			synchronized (player) {
				if (player.getUnionId() > 0 ) {
					return null;
				}
				unionManager.setUnionIdAdd(player, unionId, 0);
			}

			player.clacProperties();
			player.commitSync();
			Context.getTaskServer().autoRaidioTask(session.getUid(), 1);

			// 添加“帮派-加入帮派”日志
			LogServer.recordLog(player, ConstantsLog.union, 60016, player.getPlayerName(), union.getName());

			return ResponseMessage.getSuccMessage(union);
		case 2:
			int timestamp = TimeProcesser.getUnixTime();
			Map<Long, Integer> applyMap = DataSerialize.getInstance().getApplyMember().get(union.getId());
			if (applyMap == null) {
				applyMap = new HashMap<Long, Integer>();
				applyMap.put(session.getUid(), timestamp);
				DataSerialize.getInstance().getApplyMember().put(union.getId(), applyMap);
				// DataSerialize.getInstance().commitSync();//不重要不同步
			} else {
				applyMap.put(session.getUid(), timestamp);
			}

			player.getDataInfo().putApplyUnion(union.getId(), timestamp);
			return ResponseMessage.ticpMsg(LanguageLib.get("union.apply.suc"));
		default:
			return ResponseMessage.ticpErrMsg(LanguageLib.get("union.apply.exclude"));
		}
	}

	/**
	 * 撤销申请
	 * 
	 * @param session
	 * @param unionId
	 * @return
	 */
	public ResponseMessage cancelApply(Session session, int unionId) {

		long playerId = session.getUid();
		Player player = Context.getPlayerServer().getByPlayerId(playerId);
		player.getDataInfo().removeApplyUnion(unionId);
		Union union = Context.getUnionServer().getUnionById(unionId);
		if (union != null) {
			Map<Long, Integer> applyMap = DataSerialize.getInstance().getApplyMember().get(union.getId());
			if (!BlankUtils.isBlank(applyMap)) {
				applyMap.remove(player.getPlayerId());
				// DataSerialize.getInstance().commitSync();
			}
		}
		player.getDataInfo().commitSync();

		return ResponseMessage.getSuccMessage(true);
	}

	/**
	 * 处理申请
	 * 
	 * @param session
	 * @param pass
	 * @return
	 */
	public ResponseMessage processApply(Session session, long applyId, boolean pass) {

		Player currentPlayer = Context.getPlayerServer().getByPlayerId(session.getUid());
		Union union = Context.getUnionServer().getUnionById(currentPlayer.getUnionId());
		if (union == null) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.args"));
		}

		if (unionManager.checkPermission(session.getUid(), Union.AUDIT)) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("union.apply.perm"));
		}

		Map<Long, Integer> applyMap = DataSerialize.getInstance().getApplyMember().get(union.getId());
		if (BlankUtils.isBlank(applyMap)) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("union.apply.empty"));
		}

		List<Long> playerIds = new ArrayList<Long>();
		if (applyId == 0) {

			if (pass && unionManager.checkUnionMemberLimit(union, applyMap.size())) {
				return ResponseMessage.ticpErrMsg(LanguageLib.get("union.apply.limitsed"));
			}

			for (Long id : applyMap.keySet()) {
				Player player = Context.getPlayerServer().getByPlayerId(id);

				if (pass && unionManager.checkQuitCountsLimit(player)) {
					return ResponseMessage.ticpErrMsg(LanguageLib.get("union.apply.quited", player.getPlayerName()));
				}

				if (player.getUnionId() == 0) {
					playerIds.add(id);
				}
			}
		} else {
			Player player = Context.getPlayerServer().getByPlayerId(applyId);

			if (pass && unionManager.checkQuitCountsLimit(player)) {
				return ResponseMessage.ticpErrMsg(LanguageLib.get("union.apply.quited", player.getPlayerName()));
			}

			if (pass && unionManager.checkUnionMemberLimit(union)) {
				return ResponseMessage.ticpErrMsg(LanguageLib.get("union.apply.limited"));
			}

			if (pass && player.getUnionId() != 0) {
				return ResponseMessage.ticpErrMsg(LanguageLib.get("union.apply.existed"));
			}

			playerIds.add(applyId);
		}

		if (!pass) {
			for (long playerId : playerIds) {
				Player player = Context.getPlayerServer().getByPlayerId(playerId);
				player.getDataInfo().removeApplyUnion(union.getId());
				applyMap.remove(playerId);
				Context.getTcpServer().broadcastResMsg(ResponseMessage.ticpMsg(LanguageLib.get("union.apply.noPass", union.getName()), union.getId()), playerId);
			}
			if (playerIds.size() == 1) {
				return ResponseMessage.ticpMsg(LanguageLib.get("union.processapply.noPass"));
			}
		} else {
			for (long playerId : playerIds) {
				Player player = Context.getPlayerServer().getByPlayerId(playerId);
				UnionMember member;
				synchronized (player) {
					if (player.getUnionId() > 0) {
						continue;
					}
				    member = unionManager.setUnionIdAdd(player, union.getId(), 0);
				}
				player.clacProperties();
				player.commitSync();

				Context.getTaskServer().autoRaidioTask(playerId, 1);

				// 添加“帮派-加入帮派”日志
				LogServer.recordLog(player, ConstantsLog.union, 60016, player.getPlayerName(), union.getName());

				Map<String, Object> resultMap = new HashMap<String, Object>();
				resultMap.put("unionId", union.getId());
				resultMap.put("unionName", union.getName());

				resultMap.put("flag", member.getFlag());
				resultMap.put("permission", ConstantsConfig.getUnionJobsFlag()[member.getPosition()]);
				Context.getTcpServer().broadcast(resultMap, ConstantsRadio.unionIdUpdateRadio, player.getPlayerId());

				// 申请通过提示
				Context.getTcpServer().broadcastResMsg(ResponseMessage.ticpMsg(LanguageLib.get("union.apply.pass", union.getName()), union.getId()),
						player.getPlayerId());
			}

			if (applyId == 0) {
				return ResponseMessage.ticpMsg(LanguageLib.get("union.processapply.alljoined", playerIds.size()), true);
			}
		}
		return ResponseMessage.ticpMsg(LanguageLib.get("comm.suc.exec"));
	}

	/**
	 * 获取帮派职位架构
	 * 
	 * @param session
	 * @return
	 */
	public ResponseMessage getJobsList(Session session) {

		Union union = Context.getUnionServer().getUnionByPlayerId(session.getUid());
		UnionConfig unionConfig = unionManager.getUnionConfigByLevel(union.getLevel());
		Map<Integer, List<UnionMemberVO>> jobs = new HashMap<Integer, List<UnionMemberVO>>();
		for (UnionMember member : union.getMembers()) {
			if (jobs.containsKey(member.getPosition())) {
				jobs.get(member.getPosition()).add(member.toUnionMemberVO());
			} else {
				List<UnionMemberVO> members = new ArrayList<UnionMemberVO>();
				members.add(member.toUnionMemberVO());
				jobs.put(member.getPosition(), members);
			}
		}

		Map<String, Object> resultMap = new HashMap<String, Object>();

		for (Integer job : unionConfig.getJobMap().keySet()) {
			resultMap.put(job.toString().concat(",").concat(unionConfig.getJobMap().get(job).toString()), jobs.get(job));
		}

		return ResponseMessage.getSuccMessage(resultMap);
	}

	/**
	 * 退出帮派
	 * 
	 * @param session
	 *            当前会话
	 * @return 返回执行结果
	 */
	public ResponseMessage quitUnion(Session session) {

		Union union = Context.getUnionServer().getUnionByPlayerId(session.getUid());
		if (union == null) {
			return null;
		}
		if (session.getUid() == union.getPlayerId()) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("union.quit.main"));
		}

		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());

		UnionMember member = union.removeMember(session.getUid());
		unionManager.setQuitCounts(player);
		unionManager.deleteUnionMember(member);
		SiegeWar.removePlayer(session.getUid()); // 移除围城玩家

		// 添加”帮派-退出帮派“日志
		LogServer.recordLog(player, ConstantsLog.union, 60011, player.getPlayerName(), union.getName());

		return ResponseMessage.getSuccMessage(true);
	}

	/**
	 * 升级帮派
	 * 
	 * @param session
	 *            当前会话
	 * @return 返回执行结果
	 */
	public ResponseMessage upgradeUnion(Session session) {

		if (unionManager.checkPermission(session.getUid(), Union.UPGRADE)) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("union.upgrade.perm"));
		}

		Union union = Context.getUnionServer().getUnionByPlayerId(session.getUid());

		if (union == null) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.args"));
		}

		synchronized (union) {
			UnionConfig unionConfig = unionManager.getUnionConfigByLevel(union.getLevel() + 1);

			if (unionConfig == null) {
				return ResponseMessage.ticpErrMsg(LanguageLib.get("union.upgrade.max"));
			}

			if (union.getAsset() < unionConfig.getLevelAsset()) {
				return ResponseMessage.ticpErrMsg(LanguageLib.get("union.upgrade.asset"));
			}

			if (union.getResource() < unionConfig.getLevelResource()) {
				return ResponseMessage.ticpErrMsg(LanguageLib.get("union.upgrade.resource"));
			}

			union.setLevel(unionConfig.getLevel());
			union.alertAsset(-unionConfig.getLevelAsset());
			union.setResource(union.getResource() - unionConfig.getLevelResource());
			unionManager.upgradeUnionLevel(union);

			Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
			Context.getUnionServer().addAssetLog(union.getId(), player.getPlayerId(), player.getPlayerName(), -unionConfig.getLevelAsset(), 0,
					UnionAsset.upgradeLevel);

			// 添加“帮派-帮派升级”日志
			LogServer.recordLog(player, ConstantsLog.union, 60003, unionConfig.getLevel(), unionConfig.getLevelAsset(), unionConfig.getLevelResource());

			return ResponseMessage
					.ticpMsg(LanguageLib.get("union.upgrade.suc", unionConfig.getLevelAsset(), unionConfig.getLevelResource()), union.toUnionVO());
		}
	}

	/**
	 * 获取升级信息
	 * 
	 * @param session
	 *            当前会话
	 * @return 返回升级信息
	 */
	public ResponseMessage getUpgradeInfo(Session session) {

		Union union = Context.getUnionServer().getUnionByPlayerId(session.getUid());

		if (union == null) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.args"));
		}

		int level = union.getLevel();
		UnionConfig preLevelConfig = unionManager.getUnionConfigByLevel(level);
		UnionConfig unionConfig = unionManager.getUnionConfigByLevel(level + 1);

		if (unionConfig == null) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("union.upgrade.max"));
		}

		UnionConfigVO config = unionConfig.toUnionConfigVO();

		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("preConfig", preLevelConfig);
		resultMap.put("nextConfig", config);
		return ResponseMessage.getSuccMessage(resultMap);
	}

	/**
	 * 获取帮派技能升级信息
	 * 
	 * @param session
	 * @return
	 */
	public ResponseMessage getUnionSkilkInfo(Session session, Integer type) {

		Union union = Context.getUnionServer().getUnionByPlayerId(session.getUid());

		if (union == null) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.args"));
		}

		if (unionManager.checkPermission(session.getUid(), Union.TECHSKILL)) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("union.upgradeTech.perm"));
		}

		int level = 0;
		if (!BlankUtils.isBlank(union.getSkillMap())) {
			level = union.getSkillMap().get(type).intValue();
		}

		UnionSkillConfig preConfig = unionManager.getUnionSkillConfig(String.valueOf(type) + level);
		UnionSkillConfig nextConfig = unionManager.getUnionSkillConfig(String.valueOf(type) + (level + 1));

		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("preConfig", preConfig);
		resultMap.put("nextConfig", nextConfig);
		return ResponseMessage.getSuccMessage(resultMap);
	}

	/**
	 * 升级帮派技能
	 * 
	 * @param session
	 * @param type
	 * @return
	 */
	public ResponseMessage upgradeUnionSkill(Session session, Integer type) {

		Union union = Context.getUnionServer().getUnionByPlayerId(session.getUid());

		if (union == null) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.args"));
		}

		if (unionManager.checkPermission(session.getUid(), Union.TECHSKILL)) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("union.upgradeTech.perm"));
		}

		int level = 1;
		if (!BlankUtils.isBlank(union.getSkillMap())) {
			level = union.getSkillMap().get(type).intValue() + 1;
		}

		synchronized (union) {

			UnionSkillConfig nextConfig = unionManager.getUnionSkillConfig(String.valueOf(type) + level);

			if (nextConfig == null) {
				return ResponseMessage.ticpErrMsg(LanguageLib.get("union.upgrade.max"));
			}

			if (union.getLevel() < nextConfig.getRequiedLevel()) {
				return ResponseMessage.ticpErrMsg(LanguageLib.get("union.upgrade.level"));
			}

			if (union.getAsset() < nextConfig.getAsset()) {
				return ResponseMessage.ticpErrMsg(LanguageLib.get("union.upgrade.asset"));
			}

			if (union.getResource() < nextConfig.getResource()) {
				return ResponseMessage.ticpErrMsg(LanguageLib.get("union.upgrade.resource"));
			}

			union.setSkillLevel(type, level);
			union.alertAsset(-nextConfig.getAsset());
			union.setResource(union.getResource() - nextConfig.getResource());
			unionManager.upgradeUnionSkillLevel(union);

			Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
			Context.getUnionServer().addAssetLog(union.getId(), player.getPlayerId(), player.getPlayerName(), -nextConfig.getAsset(), 0, UnionAsset.upgradeSkill);

			// 添加“帮派-帮派升级”日志
			LogServer
					.recordLog(player, ConstantsLog.union, 60002, nextConfig.getName(), nextConfig.getLevel(), nextConfig.getAsset(), nextConfig.getResource());

			Map<String, Object> resultMap = new HashMap<String, Object>();
			resultMap.put("preConfig", nextConfig);
			resultMap.put("nextConfig", unionManager.getUnionSkillConfig(String.valueOf(type) + (level + 1)));
			resultMap.put("resource", union.getResource());
			resultMap.put("asset", union.getAsset());
			return ResponseMessage.ticpMsg(LanguageLib.get("union.upgrade.suc", nextConfig.getAsset(), nextConfig.getResource()), resultMap);
		}
	}

	/**
	 * 发布公告
	 * 
	 * @param session
	 *            当前会话
	 * @param notice
	 *            公告内容
	 * @return 返回执行结果
	 */
	public ResponseMessage publishNotice(Session session, String notice) {

		if (BlankUtils.isBlank(notice)) {
			return null;
		}

		if (unionManager.checkPermission(session.getUid(), Union.PULISH)) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("union.publish.perm"));
		}

		notice = WordProcesser.filterWorlds(notice);
		if (notice.length() > 200) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("union.publish.length", 200));
		}

		String bad = WordProcesser.chkBadWorlds(notice);
		if (!BlankUtils.isBlank(bad)) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("union.publish.bad", bad));
		}

		Union union = Context.getUnionServer().getUnionByPlayerId(session.getUid());
		union.setNotice(notice);
		unionManager.publishNotice(union);

		return ResponseMessage.getSuccMessage(union.getNotice());
	}

	/**
	 * 设置帮派
	 * 
	 * @param session
	 *            当前会话
	 * @param paramMap
	 *            参数
	 * @return 返回执行结果
	 */
	public ResponseMessage settingUnion(Session session, Map<String, Object> paramMap) {

		Union union = Context.getUnionServer().getUnionByPlayerId(session.getUid());

		if (paramMap.containsKey("validMode")) {

			if (unionManager.checkPermission(session.getUid(), Union.SETTING)) {
				return ResponseMessage.ticpErrMsg(LanguageLib.get("union.settingUnion.uperm"));
			}

			union.setValidMode((short) Integer.parseInt(paramMap.get("validMode").toString()));

			unionManager.settingUnion(union);
		}

		return ResponseMessage.getSuccMessage(union.toUnionVO());
	}

	/**
	 * 设置成员资料（职位，权限等）
	 * 
	 * @param session
	 *            当前会话
	 * @param param
	 *            成员参数
	 * @return 返回执行结果
	 */
	public ResponseMessage settingMember(Session session, Map<String, Object> paramMap) {

		if (!paramMap.containsKey("playerId")) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.args"));
		}

		Union union = Context.getUnionServer().getUnionByPlayerId(session.getUid());
		if (union == null) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.args"));
		}

		if (unionManager.checkPermission(session.getUid(), Union.ASSIGN)) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("union.settingMember.perm"));
		}

		long playerId = Double.valueOf(paramMap.get("playerId").toString()).longValue();
		if (union.getPlayerId() == playerId) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.args"));
		}

		boolean change = false;
		UnionMember player = union.getMember(session.getUid());
		UnionMember playerMember = union.getMember(playerId);

		if (playerMember == null) {
			return ResponseMessage.ticpMsg(LanguageLib.get("union.settingMember.quit"));
		}

		int originalPos = playerMember.getPosition();

		if (paramMap.containsKey("jobId")) {
			int jobId = Integer.parseInt(paramMap.get("jobId").toString());
			UnionConfig unionConfig = unionManager.getUnionConfigByLevel(union.getLevel());
			if (!unionConfig.getJobMap().keySet().contains(Integer.valueOf(jobId))) {
				return ResponseMessage.ticpErrMsg(LanguageLib.get("union.settingMember.notenough"));
			}

			if (playerMember.getPosition() == jobId) {
				return null;
			}

			if (player.getPosition() <= playerMember.getPosition() || player.getPosition() <= jobId) {
				return ResponseMessage.ticpErrMsg(LanguageLib.get("union.settingMember.err"));
			}

			Integer max = unionConfig.getJobMap().get(Integer.valueOf(jobId));
			if (max.intValue() > 0) {
				int counts = 0;
				for (UnionMember member : union.getMembers()) {
					if (member.getPosition() == jobId) {
						counts++;
						if (counts >= max.intValue()) {
							return ResponseMessage.ticpErrMsg(LanguageLib.get("union.settingMember.jobs"));
						}
					}
				}
			}

			playerMember.setPosition(jobId);
			change = true;
		}

		if (paramMap.containsKey("flag")) {
			String[] flags = paramMap.get("flag").toString().split(",");
			int flag = playerMember.getFlag();
			for (String f : flags) {
				flag |= Integer.parseInt(f);
			}

			playerMember.setFlag(flag);
			change = true;
		}

		if (change) {
			unionManager.settingMember(playerMember);

			Map<String, Object> resultMap = new HashMap<String, Object>();
			resultMap.put("unionId", playerMember.getUnionId());
			resultMap.put("flag", playerMember.getFlag());
			resultMap.put("permission", ConstantsConfig.getUnionJobsFlag()[playerMember.getPosition()]);
			Context.getTcpServer().broadcast(resultMap, ConstantsRadio.unionIdUpdateRadio, playerMember.getPlayerId());

			// 添加”帮派-任命/撤销职位“日志
			Player p = Context.getPlayerServer().getByPlayerId(session.getUid());
			LogServer.recordLog(p, ConstantsLog.union, 60012, p.getPlayerName(), LangConfigLib.getByIndex("config.union.job", originalPos), Context.getPlayerServer()
					.getByPlayerId(playerId).getPlayerName(), LangConfigLib.getByIndex("config.union.job",Integer.parseInt(paramMap.get("jobId").toString())) );
		}
		

		return ResponseMessage.getSuccMessage(true);
	}

	/**
	 * 获取资金记录
	 * 
	 * @param session
	 * @param style
	 *            1:增加,2:消耗
	 * @return
	 */
	public ResponseMessage getUnionAssets(Session session, int style) {

		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());

		if (player.getUnionId() <= 0) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.args"));
		}

		Union union = Context.getUnionServer().getUnionByPlayerId(session.getUid());

		if (union == null) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.args"));
		}

		List<UnionAsset> assets = new ArrayList<UnionAsset>(1);

		switch (style) {
		case 1:
			assets = unionManager.getAddUnionAssets(union.getId());
			break;
		case 2:
			assets = unionManager.getExpendUnionAssets(union.getId());
			break;
		default:
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.args"));
		}

		return ResponseMessage.getSuccMessage(assets);
	}

	// ======================= 摇钱树 ==========================

	/**
	 * 查看帮派摇钱树
	 * 
	 * @param session
	 * @return
	 */
	public ResponseMessage getCashCow(Session session) {

		Union union = Context.getUnionServer().getUnionByPlayerId(session.getUid());
		if (union == null) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.args"));
		}

		UnionMember member = union.getMember(session.getUid());

		// 判断上次领取是否为当天
		int counts = ConstantsModel.rewardDaily;
		if (DateFormatUtils.formatDate(new Date(TimeProcesser.getTime(member.getGetTime()))).equals(DateFormatUtils.getFormatDate())) {
			if (member.getCounts() >= ConstantsModel.rewardDaily) {
				counts = 0;
			} else {
				counts = counts - member.getCounts();
			}
		}

		UnionCashCow cashCow = unionManager.getUnionCashCow(union.getId());
		return ResponseMessage.getSuccMessage(cashCow.toUnionCashCowVO(member.getWaterTime(), member.getGetTime(), counts));
	}

	/**
	 * 浇水
	 * 
	 * @param sessioin
	 * @return
	 */
	public ResponseMessage watering(Session session) {

		Union union = Context.getUnionServer().getUnionByPlayerId(session.getUid());
		if (union == null) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.args"));
		}

		UnionMember member = union.getMember(session.getUid());

		if (TimeProcesser.getUnixTime() - member.getWaterTime() < ConstantsModel.waterIntervalTime) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("union.tree.waterInterval"));
		}

		UnionCashCow cashCow = unionManager.getUnionCashCow(union.getId());

		synchronized (union) {
			int growValue = cashCow.getGrowValue() + 1;
			if (growValue >= ConstantsModel.growValue[3]) {
				cashCow.setGrowValue(0);
				cashCow.setRewardCounts(cashCow.getRewardCounts() + ConstantsModel.rewardCounts);
				unionManager.updateUnionCashCowReward(cashCow);
			} else {
				cashCow.setGrowValue(growValue);
			}

			unionManager.updateUnionCashGrowValue(cashCow);
		}

		member.setWaterTime(TimeProcesser.getUnixTime());
		unionManager.watering(member);
//		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
		// 记录浇水次数  结束：11-13
//		player.getDataInfo().getEveryDay().intDataAlter(ConstantsDayIntKey.utw, 1);
//		player.getDataInfo().commitSync();
		return ResponseMessage.getSuccMessage(cashCow.toUnionCashCowVO(member.getWaterTime()));
	}

	/**
	 * 领取摇钱树奖励
	 * 
	 * @param session
	 * @return
	 */
	public ResponseMessage getCashCowReward(Session session) {

		Union union = Context.getUnionServer().getUnionByPlayerId(session.getUid());
		if (union == null) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.args"));
		}

		UnionMember member = union.getMember(session.getUid());
		if (TimeProcesser.getUnixTime() - member.getAddTime() < ConstantsModel.joinIntervalTime) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("union.tree.joinInterval"));
		}

		if (TimeProcesser.getUnixTime() - member.getGetTime() < ConstantsModel.rewardIntervalTime) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("union.tree.rewardInterval"));
		}

		// 判断上次领取是否为当天
		int counts = 1;
		if (DateFormatUtils.formatDate(new Date(TimeProcesser.getTime(member.getGetTime()))).equals(DateFormatUtils.getFormatDate())) {
			if (member.getCounts() >= ConstantsModel.rewardDaily) {
				return ResponseMessage.ticpErrMsg(LanguageLib.get("union.tree.rewardDaily", ConstantsModel.rewardDaily));
			} else {
				counts = member.getCounts() + 1;
			}
		}

		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());

		UnionCashCow cashCow = unionManager.getUnionCashCow(union.getId());

		synchronized (union) {
			if (cashCow.getRewardCounts() > 0) {

				// 领取帮派资金
				union.alertAsset(100);
				Context.getUnionServer().addAssetLog(union.getId(), player.getPlayerId(), player.getPlayerName(), 100, 0, UnionAsset.getCashCowReward);
				unionManager.updateUnionAsset(union);

				// 领取绑定金币
				player.alterAsset(ConstantsModel.rewardAsset);

				// 更新奖励份数
				cashCow.setRewardCounts(cashCow.getRewardCounts() - 1);
				unionManager.updateUnionCashCowReward(cashCow);
			} else {
				return ResponseMessage.ticpErrMsg(LanguageLib.get("union.tree.rewarderr"));
			}
		}

		member.setCounts(counts);
		member.setGetTime(TimeProcesser.getUnixTime());
		unionManager.updateCashCowReward(member);
		player.commitSync();

		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("asset", union.getAsset());
		resultMap.put("addAsset", ConstantsModel.rewardAsset);
		resultMap.put("playerAsset", player.getAsset());
		resultMap.put("cashCow", cashCow.toUnionCashCowVO(member.getGetTime(), ConstantsModel.rewardDaily - counts));

		// 添加”帮派-领取摇钱树奖励“日志
		LogServer.recordLog(player, ConstantsLog.union, 60014, player.getPlayerName(), counts, 10, 100);

		return ResponseMessage.getSuccMessage(resultMap);
	}

	/**
	 * 获取帮派商店数据
	 * 
	 * @param session
	 * @return
	 */
	public ResponseMessage getUnionShopList(Session session) {

		Union union = Context.getUnionServer().getUnionByPlayerId(session.getUid());
		if (union == null) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("union.no.control"));
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("list", UnionShopConfig.getAll(union.getLevel()));
		map.put("resource", union.getMember(session.getUid()).getResource());
		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
		QQDC.getInstance().logClick(player, LogFieldType.shopUnion);

		return ResponseMessage.getSuccMessage(map);
	}

	/**
	 * 帮派商店兑换
	 * 
	 * @param session
	 * @param goodsId
	 * @param counts
	 * @return
	 */
	@AccessFlag(ConstantsFlag.unionShop)
	public ResponseMessage unionShopExchange(Session session, int goodsId, int counts) {

		UnionShopConfig config = UnionShopConfig.getOne(goodsId);
		if (counts < 1 || BlankUtils.isBlank(config)) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.args"));
		}

		Union union = Context.getUnionServer().getUnionByPlayerId(session.getUid());
		if (union == null) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("union.no.control"));
		}

		UnionMember member = union.getMember(session.getUid());
		if (member == null) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("union.shop.member", union.getName()));
		}

		if (union.getLevel() < config.getLevel()) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("union.shop.level", config.getLevel()));
		}

		int resource = config.getValue() * counts;
		if (member.getResource() < resource) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("union.shop.resource"));
		}

		GoodsMode goodsMode = Context.getGoodsServer().getModeById(goodsId);
		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
		int size = NumberProcesser.ceil(counts, goodsMode.getMaxStack());
		if (!player.getPlayerBoxs().checkBlack(size)) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.player.boxmin", size));
		}

		member.setResource(member.getResource() - resource);
		Context.getUnionServer().updateMemberResource(member);
		Context.getGoodsServer().giveGoodsBing(session.getUid(), config.getGoodsId(), counts, 1);

		LogServer.recordLog(player, ConstantsLog.union, 60015, config.getName(), goodsId, counts, resource, member.getResource());

		return ResponseMessage.getSuccMessage(member.getResource());
	}
	
	/**
	 * 帮派神兽显示
	 * 
	 * @param session
	 * @return
	 */
	public ResponseMessage unionPetShow(Session session) {
		Union union = Context.getUnionServer().getUnionByPlayerId(session.getUid());
		if (union == null) {
			return null;
		}
		List<Object[]> petRanking = new ArrayList<Object[]>();
		List<Union> orderUnions = unionManager.getByUnionPetOrder();
		int orderId = 0;
		if (orderUnions != null) {
			for (Union e : orderUnions) {
				orderId++;
				Object[] orderObj = new Object[4];
				orderObj[0] = orderId;
				orderObj[1] = e.getName();
				orderObj[2] = e.getPetLv();
				long playerId = UnionPetBattle.getInstance().getLastAkPlayer(e.getId());
				Player p = Context.getPlayerServer().getByPlayerId(playerId);
				if (p != null) {
					orderObj[3] = p.getPlayerName();
				}
				
				petRanking.add(orderObj);
			}
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("petLv", union.getPetLv());
		boolean canChalleng = UnionPetBattle.getInstance().isOpened(union.getId());
		boolean petDead = canChalleng && UnionPetBattle.getInstance().getUnionPet(union.getId()) == null ? true : false;
		map.put("canChalleng", canChalleng);
		map.put("petDead",  petDead);
		map.put("petRanking", petRanking);
		List<Object[]> list = UnionPetBattle.getInstance().getUnionAkRanking(union.getId());
		map.put("akRanking", list);
		UnionPetConfig config = UnionPetConfig.getOne(union.getPetLv());
		UnionPetConfig nextConfig = UnionPetConfig.getOne(union.getPetLv() + 1);
		map.put("openRes", config != null ? config.getOpenNeedRes() : 0);
		map.put("openAsset", config != null ? config.getOpenNeedAsset() : 0);
		map.put("upRes", nextConfig != null ? nextConfig.getUpNeedRes() : 0);
		map.put("upAsset", nextConfig != null ? nextConfig.getUpNeedAsset() : 0);
		return ResponseMessage.getSuccMessage(map);
	}
	
	/**
	 * 进入神兽战斗场景
	 * 
	 * @param session
	 * @return
	 */
	public ResponseMessage uPetSceneLoad(Session session) {
		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
		if (TimeProcesser.getUnixTime() - player.getDataInfo().intDataGet(ConstantsIntKey.QUnionTime) <= 90000) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("union.pet.time"));
		}
		Union union = Context.getUnionServer().getUnionByPlayerId(session.getUid());
		if (union == null) {
			return null;
		}
		if (union.getLevel() < ConstantsLevel.olv3unionPet) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("union.pet.level"));
		}
		if (!PositionInspection.checkMainScene(player)) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.scenemain"));
		}		
		UnionPet unionPet = UnionPetBattle.getInstance().getUnionPet(union.getId());
		if (unionPet != null) {
			unionPet.broadcastOrder(session.getUid());
			unionPet.addPlayer(session.getUid());
		}
		QQDC.getInstance().logClick(player, LogFieldType.unionPet);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("reviveGold", ConstantsModel.uPetReviveCost);
		map.put("buffAdd", unionPet == null ? 0 : unionPet.getBuff(session.getUid()));
		map.put("awardCoff", unionPet == null ? 0 : unionPet.getAwardCoff(session.getUid()));
		map.put("reviveTime", unionPet == null ? 0 : unionPet.playerDeadGet(session.getUid()));
		map.put("myDam", unionPet == null ? 0 : unionPet.getDamById(session.getUid()));
		
		return ResponseMessage.getSuccMessage(map);
	}

	/**
	 * 升级帮派神兽
	 * 
	 * @param session
	 * @return
	 */
	@AccessFlag(ConstantsFlag.unionPet)
	public ResponseMessage upgradePetLv(Session session) {
		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
		Union union = Context.getUnionServer().getUnionByPlayerId(session.getUid());
		if (union == null ) {
			return null;
		}
		if (union.getLevel() < ConstantsLevel.olv3unionPet) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("union.pet.level"));
		}
		
		if (unionManager.checkPermission(session.getUid(), Union.UNIONPET)) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("union.pet.permission"));
		}
		UnionPet unionPet = UnionPetBattle.getInstance().getUnionPet(union.getId());
		if (unionPet != null) {
			return null;
		}
		synchronized (union) {
			int level = union.getPetLv();
			
			if (level >= union.getLevel()) {
				return ResponseMessage.ticpErrMsg(LanguageLib.get("union.pet.levelover"));
			}
			
			UnionPetConfig config = UnionPetConfig.getOne(level + 1);
			if (config == null) {
				return null;
			}
			if (union.getResource() < config.getUpNeedRes()) {
				return ResponseMessage.ticpErrMsg(LanguageLib.get("union.pet.lessres"));
			}
			
			if (union.getAsset() < config.getUpNeedAsset()) {
				return ResponseMessage.ticpErrMsg(LanguageLib.get("union.pet.lessasset"));
			}
			
			union.alertAsset(-config.getUpNeedAsset());
			union.setResource(union.getResource() - config.getUpNeedRes());
			
			union.setPetLv(level + 1);
			unionManager.updatePetLv(union);
			
			Context.getUnionServer().addAssetLog(union.getId(), player.getPlayerId(), player.getPlayerName(), -config.getUpNeedAsset(), 0,
					UnionAsset.upgradePetLv);
			
			LogServer.recordLog(player, ConstantsLog.union, 60102, config.getUpNeedRes(), config.getUpNeedAsset(), union.getPetLv(), union.getResource(), union.getAsset());
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("petLv", union.getPetLv());
		UnionPetConfig config = UnionPetConfig.getOne(union.getPetLv());
		UnionPetConfig nextConfig = UnionPetConfig.getOne(union.getPetLv() + 1);
		map.put("openRes", config != null ? config.getOpenNeedRes() : 0);
		map.put("openAsset", config != null ? config.getOpenNeedAsset() : 0);
		map.put("upRes", nextConfig != null ? nextConfig.getUpNeedRes() : 0);
		map.put("upAsset", nextConfig != null ? nextConfig.getUpNeedAsset() : 0);
		return ResponseMessage.getSuccMessage(map);
	}
	
	/**
	 * 开启神兽挑战
	 * 
	 * @param session
	 * @return
	 */
	@AccessFlag(ConstantsFlag.unionPet)
	public ResponseMessage openPetChallenge(Session session, int type) {
		if (type < 0 || type > 1) {
			return null;
		}
		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
		Union union = Context.getUnionServer().getUnionByPlayerId(session.getUid());
		if (union == null) {
			return null;
		}
		if (union.getPetLv() < 10 && type == 1) {
			return null;
		}
		UnionPetConfig config = UnionPetConfig.getOne(union.getPetLv());
		if (config == null) {
			return null;
		}
		if (unionManager.checkPermission(session.getUid(), Union.UNIONPET)) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("union.pet.permission"));
		}
		if (!PositionInspection.checkMainScene(player)) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.scenemain"));
		}
		int coff = type == 0 ? 1 : 5;
		int costRes = coff * config.getOpenNeedRes();
		int costAsset = coff *  config.getOpenNeedAsset();
		synchronized (union) {	
			if (union.getLevel() < ConstantsLevel.olv3unionPet) {
				return ResponseMessage.ticpErrMsg(LanguageLib.get("union.pet.level"));
			}
	
			UnionPet unionPet = UnionPetBattle.getInstance().getUnionPet(union.getId());
			if (unionPet != null) {
				return ResponseMessage.ticpErrMsg(LanguageLib.get("union.pet.opened"));
			}
			
			if (union.getResource() < costRes) {
				return ResponseMessage.ticpErrMsg(LanguageLib.get("union.pet.lessres"));
			}
			
			if (union.getAsset() < costAsset) {
				return ResponseMessage.ticpErrMsg(LanguageLib.get("union.pet.lessasset"));
			}
			
			if (UnionPetBattle.getInstance().isOpened(union.getId())) {
				return ResponseMessage.ticpErrMsg(LanguageLib.get("union.pet.opened"));
			}
			
			UnionPetBattle.getInstance().openChallenge(union.getId(), type);
			UnionPetBattle.getInstance().commitSync();
			
			union.alertAsset(-costAsset);
			union.setResource(union.getResource() - costRes);
			unionManager.updateUnionResAsset(union);
			Context.getUnionServer().addAssetLog(union.getId(), player.getPlayerId(), player.getPlayerName(), -costAsset, 0,
					UnionAsset.openPetChallenge);
			
		}
		
		// 通知
		EventNotice notice = new EventNotice(ConstantsRadio.unionPet, ConstantsRadio.unionPet_open);
		notice.put("name", player.getPlayerName());
		notice.broadcast(union.getMemberIds());
		
		ChatMain.sendMessage(ConstantsChat.Chat_Union, LanguageLib.get("union.pet.openchat", TimeProcesser.getDateByUnixTime(TimeProcesser.getUnixTime())), player);
		
		LogServer.recordLog(player, ConstantsLog.union, 60101, costRes, costAsset, union.getPetLv(), union.getResource(), union.getAsset());
		Map<String, Object> map = new HashMap<String, Object>();
		boolean canChalleng = UnionPetBattle.getInstance().isOpened(union.getId());
		boolean petDead = canChalleng && UnionPetBattle.getInstance().getUnionPet(union.getId()) == null ? true : false;
		map.put("canChalleng", canChalleng);
		map.put("petDead", petDead);
		return ResponseMessage.getSuccMessage(map);
	}
	
	/**
	 * 神兽挑战buff和加成
	 * 
	 * @param session
	 * @param type
	 * 				0 buff 1 奖励加成
	 * @return
	 */
	public ResponseMessage unionPetCoff(Session session, int type) {
		if (type < 0 || type > 1) {
			return null;
		}
		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
		Union union = Context.getUnionServer().getUnionByPlayerId(session.getUid());
		if (union == null) {
			return null;
		}
		
		UnionPet unionPet = UnionPetBattle.getInstance().getUnionPet(union.getId());
		if (unionPet == null) {
			return null;
		}
		int costGold = ConstantsModel.uPetBuffCoffCost[type];
		if (player.getGoldAll() < costGold) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.gold"));
		}
		
		if (type == 0) {
			if (unionPet.getBuff(session.getUid()) >= 100) {
				return null;
			}
			unionPet.addBuff(session.getUid(), 5);
			
			EventNotice notice = new EventNotice(ConstantsRadio.battle, ConstantsRadio.battle_BuffAdd);
			notice.put("playerId", player.getPlayerId());
			notice.put("buffNow", unionPet.getBuff(session.getUid()));
			notice.broadcastScene(player);
		} else {
			if (unionPet.getAwardCoff(session.getUid()) >= 100) {
				return null;
			}
			unionPet.addAwardCoff(session.getUid(), 5);//回成数值
		}
		
		player.alterGoldAll(-costGold);
		player.commitSync();
		LogServer.costEventLog(session.getUid(), costGold, type == 0 ? ConstantsCost.uPetBuff : ConstantsCost.uPetCoff);
		LogServer.recordLog(player, ConstantsLog.union, 60103, costGold, type == 0 ? 5 : 0, type == 1 ? 5 : 0, 
				unionPet.getBuff(session.getUid()), unionPet.getAwardCoff(session.getUid()));
		Map<String, Object> map = new HashMap<String, Object>();
		int addNow = type == 0 ? unionPet.getBuff(session.getUid()) : unionPet.getAwardCoff(session.getUid());
		map.put(type == 0 ? "buffNow" : "coffNow", addNow);
		map.put("gold", player.getGold());
		map.put("goldBing", player.getGoldBing());
		return ResponseMessage.getSuccMessage(map);
	}
	
	/**
	 * 挑战帮派神兽
	 * 
	 * @param session
	 * @return
	 */
	public ResponseMessage challengePet(Session session, int monsterDeployId) {
		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
		MonsterDeploy monsterDeploy = Context.getUndercityServer().getDeployById(monsterDeployId);
		if (monsterDeploy == null) {
			return null;
		}
		if (PositionInspection.checkMonster(player, monsterDeploy) == false) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.position.err"));
		}
		
		Scene scene = Context.getSceneServer().getSceneById(monsterDeploy.getSceneId());

		int noKill = Constants.playerState1_battle | Constants.playerState2_learn | Constants.playerState4_slaughter;

		if (player.stateContains(noKill)) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("battle.err.cooling"));
		}
		if (player.stateContains(Constants.playerState1024_aiUndercity)) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("undercity.ai.err"));
		}
		UnionPet unionPet = UnionPetBattle.getInstance().getUnionPet(player.getUnionId());
		if (unionPet == null) {
			return null;
		}
		if (!unionPet.getJoinIds().contains(session.getUid())) {
			return null;
		}
		if (unionPet.playerDeadGet(session.getUid()) > TimeProcesser.getUnixTime()) {
			return null;
		}
		FighterMonster fighterMonster = unionPet.getPetFighter();
		
		if (fighterMonster == null || fighterMonster.getHp() <= 0) {
			return null;
		} 
		
		int battleId = player.getDataInfo().getBattleId();
		if (battleId > 0) {
			Battle battle = Context.getDataServer().getBattleMap().get(battleId);
			if (battle != null) {
				if (battle.getEndTime() > TimeProcesser.getUnixTime()) {
					return ResponseMessage.ticpErrMsg(LanguageLib.get("battle.err.cooling"));
				}
				if (battle.isAllReceive() || battle.getAllPlayers().size() == 1) {
					battle.clear();
				}
			}
		}
		
		int coefficient = unionPet.getBuff(player.getPlayerId());
		Battle battle = new Battle(player.getLikedFighter(coefficient), fighterMonster);
		battle.config(1, scene.getType(), 0);
		battle.setDefaultMonsterId(monsterDeploy.getDefaultMonsterId());
		battle.execBattle(Constants.battle_pve);
		battle.deployReport(monsterDeployId);
		battle.broadcast();
		// battle.clear();
		unionPet.alterDam(player.getPlayerId(), battle.getSourceDam());
		
		if (fighterMonster.getHp() <= 0) {
			unionPet.checkDead(player.getPlayerId());
			unionPet.broadcastDead();
		}
		
		if (battle.getWiner() != 1) {
			unionPet.playerDeadSet(player.getPlayerId(), TimeProcesser.getUnixTime() + battle.getCostTime() + 32);
		}
		
		player.stateAdd(Constants.playerState1_battle);
		Context.getPlayerServer().broadcaseState(player);
		player.getDataInfo().setBattleId(battle.getId());
		new PlayerStateTask(Constants.playerState1_battle, battle.getCostTime(), player);
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("reviveTime", unionPet == null ? 0 : unionPet.playerDeadGet(session.getUid()));
		map.put("akTimes", unionPet == null ? 0 : unionPet.getAkTimes(session.getUid()));
		map.put("myDam", unionPet == null ? 0 : unionPet.getDamById(session.getUid()));
		return ResponseMessage.getSuccMessage(map);
	}
	
	/**
	 * 捐献
	 * 
	 * @param session
	 * @param num
	 * @return
	 */
	@AccessFlag(ConstantsFlag.unionPet)	
	public ResponseMessage donate(Session session, int num) {
		if (num < 3000) {
			return null;
		}
		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
		Union union = Context.getUnionServer().getUnionByPlayerId(session.getUid());
		if (union == null) {
			return null;
		}
		UnionMember member = union.getMember(session.getUid());
		if (member == null) {
			return null;
		}
		if (member.getResource() < num) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("union.pet.donateless", num));
		}
		UnionConfig config = unionManager.getUnionConfigByLevel(union.getLevel());
		if (union.getResource() >= config.getCapacity()) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("union.pet.donatelimit"));
		}
		
		int alter = config.getCapacity() - union.getResource() >= num ? num : config.getCapacity() - union.getResource() ;
		
		member.setResource(member.getResource() - alter);
		Context.getUnionServer().updateMemberResource(member);
		
		player.getDataInfo().getEveryDay().intDataAlter(ConstantsDayIntKey.rdn, alter);//记录当天捐献帮贡数量
		player.getDataInfo().commitSync();
		
		Context.getUnionServer().addResource(session.getUid(), alter, 0, 0, UnionAsset.donateResource);
		LogServer.recordLog(player, ConstantsLog.union, 60105, alter, member.getResource(), union.getResource());
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("pRes", member.getResource());
		map.put("uRes", union.getResource());
		map.put("num", alter);
		return ResponseMessage.getSuccMessage(map);
	}
	
	/**
	 * 复活
	 * 
	 * @param session
	 * @return
	 */
	public ResponseMessage revive(Session session) {
		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
		UnionPet unionPet = UnionPetBattle.getInstance().getUnionPet(player.getUnionId());
		if (unionPet == null) {
			return null;
		}
		if (player.getGoldAll() < ConstantsModel.uPetReviveCost) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.gold"));
		}
		unionPet.playerDeadSet(session.getUid(), 0);
		player.alterGoldAll(-ConstantsModel.uPetReviveCost);
		player.commitSync();

		LogServer.costEventLog(session.getUid(), ConstantsModel.uPetReviveCost, ConstantsCost.uPetRevive);
		LogServer.recordLog(player, ConstantsLog.battle, 60104, ConstantsModel.uPetReviveCost);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("gold", player.getGold());
		map.put("goldBing", player.getGoldBing());
		return ResponseMessage.getSuccMessage(map);
	}
	/**
	 * 帮派福利显示
	 * 
	 * @param session
	 * @return
	 */
	public ResponseMessage welfareShow(Session session) {
		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("rdn", player.getDataInfo().getEveryDay().intDataGet(ConstantsDayIntKey.rdn));
		map.put("rdas", player.getDataInfo().getEveryDay().intDataGet(ConstantsDayIntKey.rdas));
		return ResponseMessage.getSuccMessage(map);
	}
	
	/**
	 * 领取帮派福利
	 * 
	 * @param session
	 * @return
	 */
	public ResponseMessage walfareGet(Session session, int step) {
		if (step < 1 || step > 6) {
			return null;
		}
		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
		int donateNum = player.getDataInfo().getEveryDay().intDataGet(ConstantsDayIntKey.rdn);
		if (donateNum < ConstantsModel.unionWelfareNeed[step - 1]) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("union.welfare.cant"));
		}
		int awardStep = player.getDataInfo().getEveryDay().intDataGet(ConstantsDayIntKey.rdas);
		
		int getNum = step - awardStep;
		if (getNum <= 0) {
			return null;
		}
		
		if (!player.getPlayerBoxs().checkBlack(getNum)) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.player.boxmin", getNum));
		}
		Context.getGoodsServer().giveGoodsBing(session.getUid(), 103535, getNum, 1);
		player.getDataInfo().getEveryDay().intDataSet(ConstantsDayIntKey.rdas, step);
		player.getDataInfo().commitSync();
		GoodsMode goodsMode = Context.getGoodsServer().getModeById(103535);
		LogServer.recordLog(player, ConstantsLog.union, 60106, goodsMode.getName(), getNum, donateNum);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("rdas", step);
		map.put("getNum", getNum);
		return ResponseMessage.getSuccMessage(map);
	}
}
